package com.kk.xhr.model.dao;

import com.kk.xhr.model.entity.Appraise;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

import javax.annotation.Generated;
import java.util.List;
import java.util.Optional;

import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.appraise;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.appraiseDate;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.content;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.created;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.id;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.remark;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.result;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.status;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.updated;
import static com.kk.xhr.model.dao.AppraiseDynamicSqlSupport.userId;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

@Mapper
public interface AppraiseMapper {
    @InsertProvider(type = SqlProviderAdapter.class, method = "insertMultiple")
    int insertMultiple(MultiRowInsertStatementProvider<Appraise> insertStatement);

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    BasicColumn[] selectList = BasicColumn.columnList(id, userId, appraiseDate, result, content, remark, status, created, updated);

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    long count(SelectStatementProvider selectStatement);

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    @DeleteProvider(type=SqlProviderAdapter.class, method="delete")
    int delete(DeleteStatementProvider deleteStatement);

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    @InsertProvider(type=SqlProviderAdapter.class, method="insert")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="record.id", before=false, resultType=Long.class)
    int insert(InsertStatementProvider<Appraise> insertStatement);

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("AppraiseResult")
    Optional<Appraise> selectOne(SelectStatementProvider selectStatement);

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="AppraiseResult", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.BIGINT),
        @Result(column="appraise_date", property="appraiseDate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="result", property="result", jdbcType=JdbcType.TINYINT),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.TINYINT),
        @Result(column="created", property="created", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="updated", property="updated", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Appraise> selectMany(SelectStatementProvider selectStatement);

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    @UpdateProvider(type=SqlProviderAdapter.class, method="update")
    int update(UpdateStatementProvider updateStatement);

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, appraise, completer);
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, appraise, completer);
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default int deleteByPrimaryKey(Long id_) {
        return delete(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default int insert(Appraise record) {
        return MyBatis3Utils.insert(this::insert, record, appraise, c ->
            c.map(userId).toProperty("userId")
            .map(appraiseDate).toProperty("appraiseDate")
            .map(result).toProperty("result")
            .map(content).toProperty("content")
            .map(remark).toProperty("remark")
            .map(status).toProperty("status")
            .map(created).toProperty("created")
            .map(updated).toProperty("updated")
        );
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default int insertSelective(Appraise record) {
        return MyBatis3Utils.insert(this::insert, record, appraise, c ->
            c.map(userId).toPropertyWhenPresent("userId", record::getUserId)
            .map(appraiseDate).toPropertyWhenPresent("appraiseDate", record::getAppraiseDate)
            .map(result).toPropertyWhenPresent("result", record::getResult)
            .map(content).toPropertyWhenPresent("content", record::getContent)
            .map(remark).toPropertyWhenPresent("remark", record::getRemark)
            .map(status).toPropertyWhenPresent("status", record::getStatus)
            .map(created).toPropertyWhenPresent("created", record::getCreated)
            .map(updated).toPropertyWhenPresent("updated", record::getUpdated)
        );
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default Optional<Appraise> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, appraise, completer);
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default List<Appraise> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, appraise, completer);
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default List<Appraise> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, appraise, completer);
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default Optional<Appraise> selectByPrimaryKey(Long id_) {
        return selectOne(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, appraise, completer);
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    static UpdateDSL<UpdateModel> updateAllColumns(Appraise record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(userId).equalTo(record::getUserId)
                .set(appraiseDate).equalTo(record::getAppraiseDate)
                .set(result).equalTo(record::getResult)
                .set(content).equalTo(record::getContent)
                .set(remark).equalTo(record::getRemark)
                .set(status).equalTo(record::getStatus)
                .set(created).equalTo(record::getCreated)
                .set(updated).equalTo(record::getUpdated);
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    static UpdateDSL<UpdateModel> updateSelectiveColumns(Appraise record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(userId).equalToWhenPresent(record::getUserId)
                .set(appraiseDate).equalToWhenPresent(record::getAppraiseDate)
                .set(result).equalToWhenPresent(record::getResult)
                .set(content).equalToWhenPresent(record::getContent)
                .set(remark).equalToWhenPresent(record::getRemark)
                .set(status).equalToWhenPresent(record::getStatus)
                .set(created).equalToWhenPresent(record::getCreated)
                .set(updated).equalToWhenPresent(record::getUpdated);
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default int updateByPrimaryKey(Appraise record) {
        return update(c ->
            c.set(userId).equalTo(record::getUserId)
            .set(appraiseDate).equalTo(record::getAppraiseDate)
            .set(result).equalTo(record::getResult)
            .set(content).equalTo(record::getContent)
            .set(remark).equalTo(record::getRemark)
            .set(status).equalTo(record::getStatus)
            .set(created).equalTo(record::getCreated)
            .set(updated).equalTo(record::getUpdated)
            .where(id, isEqualTo(record::getId))
        );
    }

    @Generated(value="org.mybatis.generator.api.MyBatisGenerator", comments="Source Table: appraise")
    default int updateByPrimaryKeySelective(Appraise record) {
        return update(c ->
            c.set(userId).equalToWhenPresent(record::getUserId)
            .set(appraiseDate).equalToWhenPresent(record::getAppraiseDate)
            .set(result).equalToWhenPresent(record::getResult)
            .set(content).equalToWhenPresent(record::getContent)
            .set(remark).equalToWhenPresent(record::getRemark)
            .set(status).equalToWhenPresent(record::getStatus)
            .set(created).equalToWhenPresent(record::getCreated)
            .set(updated).equalToWhenPresent(record::getUpdated)
            .where(id, isEqualTo(record::getId))
        );
    }
}
